/* -*- Mode: Actionscript; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3 -*- */
/**
@author Timothy Uruski 
@version 1.0 2007-10-10.
@description
**/

import com.criticalmass.common.geom.Fill;

class com.criticalmass.common.geom.Shape {
   /*========================================================================
   *  CONSTANTS, DEFAULTS AND PROPERTIES
   =========================================================================*/
   // Origin constants.
   public static var TOP_LEFT:String = 'top-left';
   public static var TOP:String = 'top';
   public static var TOP_RIGHT:String = 'top-right';
   public static var LEFT:String = 'left';
   public static var CENTER:String = 'center';
   public static var RIGHT:String = 'right';
   public static var BOTTOM_LEFT:String = 'bottom-left';
   public static var BOTTOM:String = 'bottom';
   public static var BOTTOM_RIGHT:String = 'bottom-right';
   
   public static var CUSTOM:String = 'custom';
   
   private static var DEFAULT_XPOS:Number = 0;
   private static var DEFAULT_YPOS:Number = 0;
   private static var DEFAULT_WIDTH:Number = 64;
   private static var DEFAULT_HEIGHT:Number = 48;
   private static var DEFAULT_ORIGIN:String = TOP_LEFT;
   
   private var __x:Number;
   private var __y:Number;
   private var __w:Number;
   private var __h:Number;
   
   private var __origin:String;
   private var __originX:Number;
   private var __originY:Number;
   
   
   /*========================================================================
   *  ACCESSORS
   =========================================================================*/
   // Coordinates
   public function set x(ARG_x:Number):Void {
      __x = ARG_x;
   }
   public function get x():Number {
      if(__x == null) __x = DEFAULT_XPOS;
      return __x;
   }
   public function set y(ARG_y:Number):Void {
      __y = ARG_y;
   }
   public function get y():Number {
      if(__y == null) __y = DEFAULT_YPOS;
      return __y;
   }
   
   // Size
   public function set width(ARG_width:Number):Void {
      __w = (ARG_width >= 0) ? ARG_width : DEFAULT_WIDTH;
   }
   public function get width():Number {
      if(__w == null) __w = DEFAULT_WIDTH;
      return __w;
   }
   public function set height(ARG_height:Number):Void {
      __h = (ARG_height >= 0) ? ARG_height : DEFAULT_HEIGHT;
   }
   public function get height():Number {
      if(__h == null) __h = DEFAULT_HEIGHT;
      return __h;
   }
   
   // Drawing origin
   public function set origin(ARG_origin:String):Void {
      switch(ARG_origin) {
         case Shape.TOP_LEFT:
         case Shape.TOP:
         case Shape.TOP_RIGHT:
         case Shape.LEFT:
         case Shape.RIGHT:
         case Shape.CENTER:
         case Shape.BOTTOM_LEFT:
         case Shape.BOTTOM:
         case Shape.BOTTOM_RIGHT:
            __origin = ARG_origin;
         break;
         default:
            var TMP_coords:Object = Shape.toCoords(ARG_origin);
            if(TMP_coords != null) {
               __origin = Shape.CUSTOM;
               __originX = TMP_coords.x;
               __originY = TMP_coords.y;
            } else {
               __origin = Shape.DEFAULT_ORIGIN;
            }
         break;
         
      }
      
   }
   public function get origin():String {
      return __origin;
   }
   
   
   /*========================================================================
   *  CONSTRUCTOR, INSTANTIATION CONTROL AND LIFECYCLE
   =========================================================================*/
   public function Shape(ARG_x:Number, ARG_y:Number, ARG_w:Number, ARG_h:Number) {
      // Different numbers of arguments will result in different parsing results.
      if(arguments.length == 4) {
         // Shape(ARG_x:Number, ARG_y:Number, ARG_w:Number, ARG_h:Number)
         // Specified x and y-positions, non-square width and height.
         x = Number(arguments.shift());
         y = Number(arguments.shift());
         width = Number(arguments.shift());
         height = Number(arguments.shift());
      } else if(arguments.length == 3) {
         // Shape(ARG_x:Number, ARG_y:Number, ARG_w:Number)
         // Specified x and y-positions, square width and height.
         x = Number(arguments.shift());
         y = Number(arguments.shift());
         width = height = Number(arguments.shift());
      } else if(arguments.length == 2) {
         // Shape(ARG_w:Number, ARG_h:Number)
         // Default x and y-positions, non-square width and height.
         x = DEFAULT_XPOS;
         y = DEFAULT_YPOS;
         width = Number(arguments.shift());
         height = Number(arguments.shift());
      } else if(arguments.length == 1) {
         // Shape(ARG_w:Number)
         // Default x and y-positions, square width and height.
         x = DEFAULT_XPOS;
         y = DEFAULT_YPOS;
         width = height = Number(arguments.shift());
      }
   }
   
   
   /*========================================================================
   *  RENDERING
   =========================================================================*/
   public function draw(ARG_target:MovieClip, ARG_origin:String, ARG_fill, ARG_linestyle):MovieClip {
      return ARG_target;
   }
   
   
   /*========================================================================
   *  ORIGIN PARSING
   =========================================================================*/
   private static function parseOrigin(ARG_origin:String):Object {
      var TMP_origin:Object = {};
      switch(ARG_origin) {
         case Shape.TOP_LEFT:
         case Shape.TOP:
         case Shape.TOP_RIGHT:
         case Shape.LEFT:
         case Shape.RIGHT:
         case Shape.CENTER:
         case Shape.BOTTOM_LEFT:
         case Shape.BOTTOM:
         case Shape.BOTTOM_RIGHT:
            TMP_origin.type = ARG_origin;
            TMP_origin.x = 0;
            TMP_origin.y = 0;
         break;
         default:
            var TMP_coords:Object = Shape.toCoords(ARG_origin);
            if(TMP_coords != null) {
               TMP_origin.type = Shape.CUSTOM;
               TMP_origin.x = TMP_coords.x;
               TMP_origin.y = TMP_coords.y;
            } else {
               TMP_origin.type = Shape.DEFAULT_ORIGIN;
               TMP_origin.x = 0;
               TMP_origin.y = 0;
            }
         break;
      }
      
      return TMP_origin;
   }
   public static function toCoords(ARG_src:String):Object {
      var TMP_coords:Object;
      var TMP_sep:String = ',';
      var TMP_split:Array = ARG_src.split(TMP_sep);
      if(TMP_split.length == 2) {
         TMP_coords = {};
         TMP_coords.x = parseFloat(TMP_split[0]);
         TMP_coords.y = parseFloat(TMP_split[1]);
      } else {
         TMP_coords = null;
      }
      
      return TMP_coords;
   }
   
   
}